package com.hspedu.newData.dmsxl.动态规划.股票问题;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */

public class LC121买卖股票的最佳时机 {


    public int maxProfit(int[] prices) {

        int min = Integer.MAX_VALUE;
        int res = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < min){
                min = prices[i];
            }
            res = Math.max(res,prices[i] - min);

        }
        return res;
    }

    public int maxProfit1(int[] prices) {

        int len = prices.length;
        if (len < 2) return 0;

        int[][] dp = new int[len][2];

        dp[0][0] = -prices[0];
        dp[0][1] = 0;

        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i-1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] + prices[i]);
        }

        return dp[len-1][1];
    }


    @Test
    public void test() {
        // 测试代码
        int i = maxProfit1(new int[]{7,6,4,3,1});
        System.out.println("=========");

    }
}
